home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / master / Examples / Shared_Lib / lib.c < prev    next >
C/C++ Source or Header  |  1994-02-01  |  4KB  |  147 lines

  1.  
  2. /*
  3.  *  LIB.C
  4.  *
  5.  *  Basic Library Resource Handling
  6.  *
  7.  *  NOTE: all data declarations should be initialized since we skip
  8.  *      normal C startup code (unless initial value is don't care)
  9.  *
  10.  *  WARNING: arguments are passed in certain registers from the assembly
  11.  *      tag file, matched to how they are declared below.  Do not change
  12.  *      the argument declarations!
  13.  */
  14.  
  15. #include "defs.h"
  16.  
  17. Prototype LibCall Library *LibInit(long);
  18. Prototype LibCall Library *LibOpen(long, Library *);
  19. Prototype LibCall long LibClose(long, Library *);
  20. Prototype LibCall long LibExpunge(long, Library *);
  21.  
  22.  
  23. Library *LibBase = NULL;    /*  Library Base pointer    */
  24. long    SegList  = 0;
  25. Library *SysBase  = NULL;    /*  EXEC calls            */
  26. struct DosLibrary *DOSBase  = NULL;    /*  if we used it ...        */
  27.  
  28. /*
  29.  *    The Initialization routine is given only a seglist pointer.  Since
  30.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  31.  *    and return either NULL or the library pointer.  Exec has Forbid()
  32.  *    for us during the call.
  33.  *
  34.  *    If you have an extended library structure you must specify the size
  35.  *    of the extended structure in MakeLibrary().
  36.  */
  37.  
  38. LibCall Library *
  39. LibInit(segment)
  40. long segment;
  41. {
  42.     Library *lib;
  43.     static const long Vectors[] = {
  44.     (long)ALibOpen,
  45.     (long)ALibClose,
  46.     (long)ALibExpunge,
  47.     (long)ALibReserved,
  48.  
  49.     (long)LockTestLib,    /*  library dependant    */
  50.     (long)UnLockTestLib,
  51.     (long)PostString,
  52.     (long)GetString,
  53.  
  54.     -1
  55.     };
  56.     SysBase = *(Library **)4;
  57.     DOSBase = OpenLibrary("dos.library", 0);
  58.  
  59.     LibBase = lib = MakeLibrary((APTR)Vectors,NULL,NULL,sizeof(Library),NULL);
  60.     lib->lib_Node.ln_Type = NT_LIBRARY;
  61.     lib->lib_Node.ln_Name = LibName;
  62.     lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  63.     lib->lib_Version  = 36;
  64.     lib->lib_Revision = 1;
  65.     lib->lib_IdString = (APTR)LibId;
  66.     SegList = segment;
  67.     AddLibrary(lib);
  68.  
  69.     InitC();
  70.  
  71.     return(lib);
  72. }
  73.  
  74. /*
  75.  *    Open is given the library pointer and the version request.  Either
  76.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  77.  *    Exec has Forbid() for us during the call.
  78.  */
  79.  
  80. LibCall Library *
  81. LibOpen(version, lib)
  82. Library *lib;
  83. long version;
  84. {
  85.     ++lib->lib_OpenCnt;
  86.     lib->lib_Flags &= ~LIBF_DELEXP;
  87.     return(lib);
  88. }
  89.  
  90. /*
  91.  *    Close is given the library pointer and the version request.  Be sure
  92.  *    not to decrement the open count if already zero.    If the open count
  93.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  94.  *    and return the seglist.  Otherwise we return NULL.
  95.  *
  96.  *    Note that this routine never sets LIBF_DELEXP on its own.
  97.  *
  98.  *    Exec has Forbid() for us during the call.
  99.  */
  100.  
  101. LibCall long
  102. LibClose(dummy, lib)
  103. long dummy;
  104. Library *lib;
  105. {
  106.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  107.     return(NULL);
  108.     if (lib->lib_Flags & LIBF_DELEXP)
  109.     return(LibExpunge(0, lib));
  110.     return(NULL);
  111. }
  112.  
  113. /*
  114.  *    We expunge the library and return the Seglist ONLY if the open count
  115.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  116.  *    flag and return NULL.
  117.  *
  118.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  119.  *    might be called from the memory allocator and thus we CANNOT DO A
  120.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  121.  *
  122.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  123.  *    therefore freeze if we called it ourselves.  As far as I can tell
  124.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  125.  *    below.
  126.  */
  127.  
  128. LibCall long
  129. LibExpunge(dummy, lib)
  130. long dummy;
  131. Library *lib;
  132. {
  133.     if (lib->lib_OpenCnt) {
  134.     lib->lib_Flags |= LIBF_DELEXP;
  135.     return(NULL);
  136.     }
  137.     Remove(&lib->lib_Node);
  138.     FreeMem((char *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  139.     if (DOSBase) {
  140.     CloseLibrary((Library *)DOSBase);
  141.     DOSBase = NULL;
  142.     }
  143.     UnInitC();
  144.     return((long)SegList);
  145. }
  146.  
  147.